%%%%%%%%%%%%%%%%%%%%%%l
%%
%% Module summary
%%
%% Test function for db2
%%
%% Input parameters;
%% 
%% [Queries,             default: 1000
%%  String_length,       default: 10
%%  socket | rpc,        default: socket
%%  test_all | Node,     default: test_all
%%  mixed | puts | gets  default: mixed
%%  ]
%%
%%%%%%%%%%%%%%%%%%%%%%%
-module(test_db2_old).
-export([setup/1,test/1,tear_down/1]).

-behaviour(testcase).

-record(state, {v1,         % v1 & v1; the values used
                v2,         % when performing queries
                connection, % socket | Node (Node is target node in rcp calls)
                type        % mixed | puts | gets               
                }).

setup(Params) ->
    % Read parameters. Doesn't do any type checking, write correct! :)
    {Queries,StrLn,Conn,Nodes,Type} = read_params(Params),
    
    % Setup connection
    % socket or rpc?
    case Conn of
        socket -> case Nodes of
                      test_all -> test_lib:socket_start();
                      Node     -> test_lib:socket_start(Node) 
                  end,
                  Target = socket; % Compiler warnings
        rpc    -> case Nodes of
                      test_all -> Target = test_lib:get_random_db_node();
                      Node     -> Target = Node
                  end
    end,

    % Setup queries
    Values = test_lib:random_key_value_pairs(Queries,StrLn),
    {Values1,Values2} = lists:split(length(Values) div 2, Values),

    #state{v1 = Values1, v2 = Values2, connection = Target, type = Type}.


test(State) ->
    FirstTimestamp = os:timestamp(),

    % Perform queries
    case State#state.connection of
        socket -> case State#state.type of
                      mixed -> test_lib:socket_put_list(State#state.v1),
                               test_lib:socket_get_list(State#state.v1);
                      puts  -> test_lib:socket_put_list(State#state.v1),
                               test_lib:socket_put_list(State#state.v2);
                      gets  -> test_lib:socket_get_list(State#state.v1),
                               test_lib:socket_get_list(State#state.v2)
                  end;
        Target  -> case State#state.type of
                      mixed -> test_lib:rpc_put_list(Target,State#state.v1),
                               test_lib:rpc_get_list(Target,State#state.v1);
                      puts  -> test_lib:rpc_put_list(Target,State#state.v1),
                               test_lib:rpc_put_list(Target,State#state.v2);
                      gets  -> test_lib:rpc_get_list(Target,State#state.v1),
                               test_lib:rpc_get_list(Target,State#state.v2)
                  end
    end,

    SecondTimeStamp = os:timestamp(),
   
    % Return resultList with predefined tuples
    [{slave, node()},
     {queries, length(State#state.v1) + length(State#state.v2)},
     {time,time_stamp_diff(FirstTimestamp,SecondTimeStamp)}].

tear_down(State) ->

    % Close socket connection
    case State#state.connection of
        socket -> test_lib:socket_stop();
        _      -> ok
    end.



read_params(Params) ->
    case length(Params) of
        0 -> {1000,10,socket,test_all,mixed};
        1 -> Queries = lists:nth(1,Params),
             {Queries,10,socket,test_all,mixed};
        2 -> Queries = lists:nth(1,Params),
             StrLn = lists:nth(2,Params),
             {Queries,StrLn,socket,test_all,mixed};
        3 -> Queries = lists:nth(1,Params),
             StrLn = lists:nth(2,Params),
             Conn = lists:nth(3,Params),
             {Queries,StrLn,Conn,test_all,mixed};
        4 -> Queries = lists:nth(1,Params),
             StrLn = lists:nth(2,Params),
             Conn = lists:nth(3,Params),
             Nodes = lists:nth(4,Params),
             {Queries,StrLn,Conn,Nodes,mixed};
        5 -> Queries = lists:nth(1,Params),
             StrLn = lists:nth(2,Params),
             Conn = lists:nth(3,Params),
             Nodes = lists:nth(4,Params),
             Type = lists:nth(5,Params),
             {Queries,StrLn,Conn,Nodes,Type}
    end.


% Borrowing Kristian's code
time_stamp_diff(Time1,Time2) ->
   {A,B,C} = Time1,
   {D,E,F} = Time2,
   {_,H,I} = {D-A,E-B,F-C},
   %% H * 1000, G * 1 billion
   H + (I / 1000000).
